/*
 * @Author: xiehangyun 
 * @Date: 2025-05-12 10:17:38 
 * @Last Modified by: 肖思汗
 * @Last Modified time: 2025-07-13 15:09:58
 */

import { get } from "lodash";
import { toJS } from "mobx";
import indexApi from "./indexApi";
import { makeAutoObservable } from 'mobx';
import { GoodsCategoryData, GoodsData, MiniProgramDecorateData } from "types/type";
import categoryApi from "../Category/categoryApi";

class IndexStore {

    constructor() {
        makeAutoObservable(this, {}, { deep: false });
        // autorun(() => {
        //     if (userStore.isLogin) {
        //         //商品一级分类菜单
        //         indexApi.firstProductType().then(res => {
        //             this.setGoodsCategoryDatas(res.data);
        //         })
        //         //获取所有的商品二级分类菜单
        //         indexApi.secondProductType().then(res => {
        //             this.setSecondProductTypes(res.data);
        //         });
        //         //查询首页配置信息
        //         indexApi.queryHomePageConfig().then(res => {
        //             this.setMiniProgramDecorateData(res.data);
        //         });
        //     }
        // }, { delay: 30 });
    }

    async init() {
        //商品一级分类菜单
        await indexApi.firstProductType().then(res => {
            this.setGoodsCategoryDatas(res.data);
        })
        //获取所有的商品二级分类菜单
        await indexApi.secondProductType().then(res => {
            this.setSecondProductTypes(res.data);
        });
        //查询首页配置信息
        await indexApi.queryHomePageConfig().then(res => {
            this.setMiniProgramDecorateData(res.data);
        });
    }

    // 商品一级分类
    goodsCategoryDatas: Array<GoodsCategoryData> = [];
    setGoodsCategoryDatas(goodsCategoryDatas: Array<GoodsCategoryData>) {
        this.goodsCategoryDatas = goodsCategoryDatas;
        const productTypeId = get(goodsCategoryDatas, [0, "id"], -1) as number;
        if (productTypeId) {
            this.setActiveFirstProductType(productTypeId);
        }
    }

    //选中的一级分类
    activeFirstProductType = -1;
    setActiveFirstProductType(activeFirstProductType: number) {
        this.activeFirstProductType = activeFirstProductType;
    }

    // 商品二级分类
    secondProductTypes: Array<GoodsCategoryData> = [];
    // 商品二级分类下的场景分类
    scene: Array<GoodsCategoryData> = [];
    setSecondProductTypes(secondProductTypes: Array<GoodsCategoryData>) {
        this.secondProductTypes = secondProductTypes;
        this.scene = toJS(secondProductTypes.filter(item => item.parentId === 48));
        // const productTypeId = get(this.scene, [0, "id"], -1) as number;
        // if (productTypeId) {
        //     this.setActiveSecondProductType(productTypeId);
        // }
        this.setActiveSecondProductType("");
    }

    // 选中的二级分类
    activeSecondProductType = "";
    setActiveSecondProductType(activeSecondProductType: string) {
        this.activeSecondProductType = activeSecondProductType;
        indexApi.queryTopProductList(activeSecondProductType || this.scene.map(s => s.id).join(",")).then((goodsList: Array<GoodsData>) => {
            this.setGoodsList(goodsList);
        });
    }

    // 商品列表
    goodsList: Array<GoodsData> = [];
    setGoodsList(goodsList: Array<GoodsData>) {
        this.goodsList = goodsList;
    }
    // goodsList: Array<any> = [
    //     {
    //         productId: 1,
    //         productName: '商品名称',
    //         pagePrice: 6,
    //         productShowImageUrl: smallLogoPNG,
    //         protection: '方便快捷好看',
    //     },
    //     {
    //         productId: 2,
    //         productName: '商品名称过长商品名…',
    //         pagePrice: 6,
    //         productShowImageUrl: smallLogoPNG,
    //         protection: '方便快捷好看',
    //     },
    //     {
    //         productId: 3,
    //         productName: '商品名称3',
    //         pagePrice: 6,
    //         productShowImageUrl: smallLogoPNG,
    //         protection: '方便快捷好看',
    //     },
    //     {
    //         productId: 4,
    //         productName: '商品名称4',
    //         pagePrice: 6,
    //         productShowImageUrl: smallLogoPNG,
    //         protection: '方便快捷好看',
    //     },
    // ];

    //查询首页配置信息
    miniProgramDecorateData: MiniProgramDecorateData = {
        globalConfigId: 3,
        ifCategoryPageHighLevelCustom: true,
        ifGoodsCategoryShow: true,
        ifHotGoodsShow: true,
        productSort: [],
        sysSlideshows: []
    }
    setMiniProgramDecorateData(miniProgramDecorateData: MiniProgramDecorateData) {
        try {
            //@ts-ignore
            miniProgramDecorateData.productSort = JSON.parse(miniProgramDecorateData.productSort || "[]");
        } catch (error) {
        }
        this.miniProgramDecorateData = miniProgramDecorateData;
    }

}



const indexStore = new IndexStore();

export default indexStore;